package com.spa.infrastructure.repository.service;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.spa.infrastructure.repository.condition.UserAddressCondition;
import com.spa.infrastructure.repository.mapper.UserAddressMapper;
import com.spa.infrastructure.repository.po.UserAddressPO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@Slf4j
public class UserAddressRepositoryService extends MPJBaseServiceImpl<UserAddressMapper, UserAddressPO> {

    /**
     * 分页
     *
     * @author: lig
     * @date: 2023/12/6
     */
    public Page<UserAddressPO> queryPage(UserAddressCondition condition) {
        LambdaQueryWrapper<UserAddressPO> wrapper = wrapper(condition);
        return page(Page.of(condition.getCurrent(), condition.getSize()), wrapper);

    }

    public List<UserAddressPO> list(UserAddressCondition condition) {
        return list(wrapper(condition));

    }

    public List<UserAddressPO> listByUserId(Long userId) {
        UserAddressCondition condition = new UserAddressCondition();
        condition.setUserId(userId);
        return list(wrapper(condition));

    }


    private LambdaQueryWrapper<UserAddressPO> wrapper(UserAddressCondition condition) {
        LambdaQueryWrapper<UserAddressPO> wrapper = Wrappers.lambdaQuery();
        wrapper.in(CollUtil.isNotEmpty(condition.getIds()), UserAddressPO::getId,condition.getIds());
        wrapper.eq(ObjectUtils.isNotEmpty(condition.getUserId()), UserAddressPO::getUserId,condition.getUserId());

        wrapper.orderByDesc(UserAddressPO::getCreateTime);
        return wrapper;
    }


    /**
     * 清楚默认值
     *
     * @author: lig
     * @date: 2023/12/13
     */
    public boolean clearDefaultByUserId(Long userId) {
        LambdaUpdateWrapper<UserAddressPO> wrapperUp = Wrappers.lambdaUpdate();
        wrapperUp.set(UserAddressPO::getIsDefault,"1");
        wrapperUp.eq(UserAddressPO::getUserId,userId);
        return update(wrapperUp);
    }
//    /**
//     * 清楚默认值
//     *
//     * @author: lig
//     * @date: 2023/12/13
//     */
//    public boolean clearDefault(List<Long> ids) {
//        LambdaUpdateWrapper<UserAddressPO> wrapperUp = Wrappers.lambdaUpdate();
//        wrapperUp.set(UserAddressPO::getIsDefault,"1");
//        wrapperUp.in(UserAddressPO::getId,ids);
//        return update(wrapperUp);
//    }


    /**
     * 设置默认值
     *
     * @author: lig
     * @date: 2023/12/13
     */
    public boolean isDefault(Long id) {
        LambdaUpdateWrapper<UserAddressPO> wrapperUp = Wrappers.lambdaUpdate();
        wrapperUp.set(UserAddressPO::getIsDefault,"0");
        wrapperUp.eq(UserAddressPO::getId,id);
        return update(wrapperUp);
    }

    /**
     * 默认地址
     *
     * @author: lig
     * @date: 2023/12/13
     */
    public UserAddressPO defaultInfoByUserId(Long userId) {
        LambdaQueryWrapper<UserAddressPO> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(UserAddressPO::getIsDefault,"0");
        wrapper.eq(UserAddressPO::getUserId,userId);
        List<UserAddressPO> poList = list(wrapper);
        if(CollUtil.isNotEmpty(poList))return poList.get(0);
        return null;
    }
//    /**
//     * 默认地址
//     *
//     * @author: lig
//     * @date: 2023/12/13
//     */
//    public UserAddressPO defaultInfo(List<Long> ids) {
//        LambdaQueryWrapper<UserAddressPO> wrapper = Wrappers.lambdaQuery();
//        wrapper.eq(UserAddressPO::getIsDefault,"0");
//        wrapper.in(UserAddressPO::getId,ids);
//        return getOne(wrapper);
//    }


    public boolean isUserAddress(Long userId,Long id){
        LambdaQueryWrapper<UserAddressPO> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(UserAddressPO::getId,id);
        wrapper.eq(UserAddressPO::getUserId,userId);
        return exists(wrapper);

    }

}
